package cool.taomu.toolkit.stock.service

import com.google.inject.Inject
import cool.taomu.toolkit.stock.dao.jooq.tables.TaomuStockHistory
import cool.taomu.toolkit.stock.dao.jooq.tables.TaomuStockList
import cool.taomu.toolkit.stock.dao.jooq.tables.TaomuStockSignal
import cool.taomu.toolkit.stock.entity.SignalDetailSummaryEntity
import cool.taomu.toolkit.stock.entity.SignalGroupEntity
import cool.taomu.toolkit.stock.entity.StockCodeEntity
import java.time.LocalDateTime
import java.util.List
import org.joda.time.DateTime
import org.jooq.DSLContext
import org.slf4j.LoggerFactory

import static org.jooq.impl.DSL.*
import cool.taomu.toolkit.cacheable.Cacheable
import cool.taomu.toolkit.stock.dao.jooq.tables.VwTaomuStockSignalSummary
import cool.taomu.toolkit.stock.dao.jooq.enums.VwTaomuStockSignalSummarySignalType
import cool.taomu.toolkit.stock.dao.jooq.tables.TaomuStockSignalSummary
import cool.taomu.toolkit.stock.dao.jooq.enums.TaomuStockSignalSummarySignalType

class StockSignalService {
    val static LOG = LoggerFactory.getLogger(StockSignalService);

    @Inject
    DSLContext dsl;

    @Cacheable
    def List<SignalGroupEntity> getSignalGroup() {
        LOG.info("获取股票信号分组");
        var List<SignalGroupEntity> groups = dsl.select(
            TaomuStockSignal.TAOMU_STOCK_SIGNAL.INDICATOR,
            TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_TYPE,
            TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_DATE,
            count().^as("count"),
            groupConcat(TaomuStockSignal.TAOMU_STOCK_SIGNAL.STOCK_CODE).^as("stock_codes")
        ).from(TaomuStockSignal.TAOMU_STOCK_SIGNAL).groupBy(
            TaomuStockSignal.TAOMU_STOCK_SIGNAL.INDICATOR,
            TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_TYPE,
            TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_DATE
        ).orderBy(
            TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_DATE.desc(),
            TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_TYPE
        ).fetchInto(SignalGroupEntity); // 自动映射
        return groups;
    }

    @Cacheable
    def llm2() {
        var day = DateTime.now().minusDays(1).toString("yyyy-MM-dd'T'00:00:00");
        var lowToDouble = TaomuStockHistory.TAOMU_STOCK_HISTORY.LOW.cast(Double);
        var subSql = dsl.select(TaomuStockHistory.TAOMU_STOCK_HISTORY.CODE).from(TaomuStockHistory.TAOMU_STOCK_HISTORY).
            where(
                TaomuStockHistory.TAOMU_STOCK_HISTORY.DATE.eq(LocalDateTime.parse(day))
            ).and(lowToDouble.gt(TaomuStockHistory.TAOMU_STOCK_HISTORY.M5)).and(
                lowToDouble.gt(TaomuStockHistory.TAOMU_STOCK_HISTORY.M10)).and(
                lowToDouble.gt(TaomuStockHistory.TAOMU_STOCK_HISTORY.M20)).and(
                lowToDouble.gt(TaomuStockHistory.TAOMU_STOCK_HISTORY.M30)).and(
                lowToDouble.gt(TaomuStockHistory.TAOMU_STOCK_HISTORY.M60)).orderBy(lowToDouble.desc);
        var res = dsl.select().from(TaomuStockList.TAOMU_STOCK_LIST).where(
            TaomuStockList.TAOMU_STOCK_LIST.CODE.in(subSql)).fetchInto(StockCodeEntity);
        var codes = res.map[it.code]
        var res2 = this.getSignalGroup2(codes);
        return res2
    }

    @Cacheable
    def List<SignalGroupEntity> getSignalGroup2(List<String> in) {
        LOG.info("获取股票信号分组");
        var List<SignalGroupEntity> groups = dsl.select(
            TaomuStockSignal.TAOMU_STOCK_SIGNAL.INDICATOR,
            TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_TYPE,
            TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_DATE,
            count().^as("count"),
            groupConcat(TaomuStockSignal.TAOMU_STOCK_SIGNAL.STOCK_CODE).^as("stock_codes")
        ).from(TaomuStockSignal.TAOMU_STOCK_SIGNAL).where(TaomuStockSignal.TAOMU_STOCK_SIGNAL.STOCK_CODE.in(in)).
            groupBy(
                TaomuStockSignal.TAOMU_STOCK_SIGNAL.INDICATOR,
                TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_TYPE,
                TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_DATE
            ).orderBy(
                TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_DATE.desc(),
                TaomuStockSignal.TAOMU_STOCK_SIGNAL.SIGNAL_TYPE
            ).fetchInto(SignalGroupEntity); // 自动映射
        return groups;
    }

    @Cacheable
    def SignalDetailSummary(String signalType) {
        if (signalType.equalsIgnoreCase("buy")) {
            return dsl.select().from(TaomuStockSignalSummary.TAOMU_STOCK_SIGNAL_SUMMARY).where(
                TaomuStockSignalSummary.TAOMU_STOCK_SIGNAL_SUMMARY.SIGNAL_TYPE.eq(
                    TaomuStockSignalSummarySignalType.buy))
                    .fetchInto(SignalDetailSummaryEntity)
        } else if (signalType.equalsIgnoreCase("sell")) {
            return dsl.select().from(TaomuStockSignalSummary.TAOMU_STOCK_SIGNAL_SUMMARY).where(
                TaomuStockSignalSummary.TAOMU_STOCK_SIGNAL_SUMMARY.SIGNAL_TYPE.eq(
                    TaomuStockSignalSummarySignalType.sell)).fetchInto(SignalDetailSummaryEntity)
        } else {
            return dsl.select().from(TaomuStockSignalSummary.TAOMU_STOCK_SIGNAL_SUMMARY).fetchInto(
                SignalDetailSummaryEntity)
        }
    }
    
     @Cacheable
    def SignalDetailSummaryPage(String signalType,Integer row,Integer offset) {
        if (signalType.equalsIgnoreCase("buy")) {
            return dsl.select().from(TaomuStockSignalSummary.TAOMU_STOCK_SIGNAL_SUMMARY).where(
                TaomuStockSignalSummary.TAOMU_STOCK_SIGNAL_SUMMARY.SIGNAL_TYPE.eq(
                    TaomuStockSignalSummarySignalType.buy)).limit(offset,row)
                    .fetchInto(SignalDetailSummaryEntity)
        } else if (signalType.equalsIgnoreCase("sell")) {
            return dsl.select().from(TaomuStockSignalSummary.TAOMU_STOCK_SIGNAL_SUMMARY).where(
                TaomuStockSignalSummary.TAOMU_STOCK_SIGNAL_SUMMARY.SIGNAL_TYPE.eq(
                    TaomuStockSignalSummarySignalType.sell)).limit(offset,row).fetchInto(SignalDetailSummaryEntity)
        } else {
            return dsl.select().from(TaomuStockSignalSummary.TAOMU_STOCK_SIGNAL_SUMMARY).limit(offset,row).fetchInto(
                SignalDetailSummaryEntity)
        }
    }

}
